﻿namespace LiteBlog.Common.Entities
{
    using LiteBlog.Common.Components;
    using System;
    using System.ComponentModel.DataAnnotations;
    using System.Xml;

    public class Comment
    {
        private string _highlightedText = string.Empty;
        private string _text = string.Empty;

        public string ID { get; set; }
        public string FileID { get; set; }
        public string Ip { get; set; }
        public bool IsApproved { get; set; }
        public bool IsAuthor { get; set; }
        public DateTime Time { get; set; }
        public string FormattedTime { get; set; } 

        [Required(ErrorMessage = "Please enter your name")]
        public string Name { get; set; }
        
          [RegularExpression(
            @"(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&amp;:/~\+#]*[\w\-\@?^=%&amp;/~\+#])?",
            ErrorMessage = "Please enter a valid web URL")]
        [UrlValid(ErrorMessage="The URL entered is not valid. The link may be broken or the server down!")]
        public string Url { get; set; }

        [Required(ErrorMessage = "Please enter your comment")]
         public string RawComment { get; set; }

    
        public string HighlightedText { get; set; }
        public string Text { get; set; }

        /// <summary>
        /// Gets the tile text.
        /// </summary>
        public string TileText
        {
            get
            {
                string text = this.Text;
                XmlDocument doc = new XmlDocument();
                try
                {
                    string xml = "<p>" + text.Replace("&nbsp;", " ") + "</p>";
                    doc.LoadXml(xml);
                    text = doc.InnerText;
                }
                catch (Exception)
                {
                }

                if (text.Length > 150)
                {
                    text = text.Substring(0, 150);
                    text += " ..";
                }

                return text;
            }
        }
    }
}